using System;
using System.Collections;
using UnityEngine;

public class PurchaseLayer : MonoBehaviour
{
	protected InAppPurchase.InAppObject item;

	protected GGInAppPurchase.PurchaseCompleteDelegate onComplete;

	public PurchaseResultSymButton symButton;

	public CurrencyPurchaseButton button;

	public GameObject waitingObject;

	public UIAnimationElement successAnimation;

	public UIAnimationElement outAnimation;

	public GameObject rotatingSprite;

	public GameObject buttonsContainer;

	public UILabel button1Label;

	public UILabel button2Label;

	public UILabel mainLabel;

	private GGInAppPurchase.PurchaseResponse response;

	public void PurchaseItem(InAppPurchase.InAppObject item, GGInAppPurchase.PurchaseCompleteDelegate onComplete)
	{
		BehaviourSingletonInit<SnapshotSyncManager>.instance.canSync = false;
		this.item = item;
		this.onComplete = onComplete;
		NavigationManager.instance.Push(base.gameObject);
		GGInAppPurchase.instance.onPurchaseComplete -= PurchaseCompleteDelegate;
		GGInAppPurchase.instance.onPurchaseComplete += PurchaseCompleteDelegate;
		InAppPurchase.instance.buyProduct(item.productId);
		if (symButton != null)
		{
			symButton.Init(item);
		}
	}

	private void OnEnable()
	{
		BehaviourSingletonInit<SnapshotSyncManager>.instance.canSync = false;
		GGInAppPurchase.instance.onPurchaseComplete -= PurchaseCompleteDelegate;
		GGInAppPurchase.instance.onPurchaseComplete += PurchaseCompleteDelegate;
		GGInAppPurchase.instance.purchaseComplete(item.productId);
	}

	private void OnDisable()
	{
		BehaviourSingletonInit<SnapshotSyncManager>.instance.canSync = true;
		GGInAppPurchase.instance.onPurchaseComplete -= PurchaseCompleteDelegate;
	}

	private void CallOnComplete(GGInAppPurchase.PurchaseResponse response)
	{
		GGInAppPurchase.PurchaseCompleteDelegate purchaseCompleteDelegate = onComplete;
		onComplete = null;
		if (purchaseCompleteDelegate != null)
		{
			purchaseCompleteDelegate(response);
		}
		else
		{
			NavigationManager.instance.Pop();
		}
	}

	private void PurchaseCompleteDelegate(GGInAppPurchase.PurchaseResponse response)
	{
		Debug.Log("Response " + response.responseCode);
		this.response = response;
		StopAllCoroutines();
		if (response.success)
		{
			ShowSuccessfullyBuyItem(response);
		}
		else if (response.responseCode == GGInAppPurchase.PurchaseResponseCode.AlreadyOwned)
		{
			ShowRestoringPurchases("Item Already Owned. ");
		}
		else if (response.responseCode == GGInAppPurchase.PurchaseResponseCode.CantVerifySignature)
		{
			ShowRestoringPurchases("Cant contact server to verify Signature. ");
		}
		else if (response.responseCode == GGInAppPurchase.PurchaseResponseCode.ConsumeFailed)
		{
			ShowRestoringPurchases("Item not consumed. ");
		}
		else if (response.responseCode == GGInAppPurchase.PurchaseResponseCode.Failed || response.responseCode == GGInAppPurchase.PurchaseResponseCode.UnknownError)
		{
			ShowRestoringPurchases("Error happened. ");
		}
		else if (response.responseCode == GGInAppPurchase.PurchaseResponseCode.SignatureNotAccepted)
		{
			ShowError("Purchase signature is not correct. ");
		}
		else
		{
			CallOnComplete(response);
		}
	}

	private void ShowRestoringPurchases(string text)
	{
		button.gameObject.SetActive(false);
		waitingObject.SetActive(true);
		rotatingSprite.SetActive(true);
		buttonsContainer.SetActive(false);
		UITools.ChangeText(mainLabel, text + "Trying Again...");
		GGInAppPurchase.instance.restorePurchases();
		StartCoroutine(WaitAndExecute(5f, delegate
		{
			Debug.Log("TIMED OUT");
			ShowError("Response timed out. ");
		}));
	}

	private IEnumerator WaitAndExecute(float duration, Action d)
	{
		yield return new WaitForSeconds(duration);
		if (d != null)
		{
			d();
		}
	}

	private void ShowError(string text)
	{
		button.gameObject.SetActive(false);
		waitingObject.SetActive(true);
		rotatingSprite.SetActive(false);
		UITools.ChangeText(mainLabel, text + "Please retry or contact us for assistance!");
		buttonsContainer.SetActive(true);
	}

	private void ShowVerifyingPurchase()
	{
		button.gameObject.SetActive(false);
		waitingObject.SetActive(true);
		rotatingSprite.SetActive(true);
		buttonsContainer.SetActive(false);
		UITools.ChangeText(mainLabel, "Please Wait...");
	}

	private void ShowSuccessfullyBuyItem(GGInAppPurchase.PurchaseResponse response)
	{
		StopAllCoroutines();
		StartCoroutine(DoShowSuccessfullyBuyItem(response));
	}

	private IEnumerator DoShowSuccessfullyBuyItem(GGInAppPurchase.PurchaseResponse response)
	{
		TableTennisMainNavigation mainNavigation = TableTennisMainNavigation.instance;
		TableTennisMainNavigation.HeaderType prevHeaderType = mainNavigation.GetActiveHeaderType();
		InAppPurchase.InAppObject item = InAppPurchase.instance.FindInAppForId(response.productId);
		button.gameObject.SetActive(true);
		waitingObject.SetActive(false);
		button.Init(item, null);
		successAnimation.Play();
		while (successAnimation.isPlaying)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.25f);
		WalletManager walletManager = GGPlayerSettings.instance.walletManager;
		if (item.getsCoins > 0)
		{
			mainNavigation.SetHeader(TableTennisMainNavigation.HeaderType.CoinsCashEnergy);
			CurrencyType currencyType3 = CurrencyType.coins;
			TableTennisCoinController coinControl3 = mainNavigation.GetCoinControlForCurrency(currencyType3);
			coinControl3.AnimateChangeMoney(button.GetComponent<UIWidget>(), walletManager.CurrencyCount(currencyType3) - item.getsCoins, walletManager.CurrencyCount(currencyType3), null);
			yield return new WaitForSeconds(0.75f);
		}
		if (item.getsDollars > 0)
		{
			mainNavigation.SetHeader(TableTennisMainNavigation.HeaderType.CoinsCashEnergy);
			CurrencyType currencyType2 = CurrencyType.ggdollars;
			TableTennisCoinController coinControl2 = mainNavigation.GetCoinControlForCurrency(currencyType2);
			coinControl2.AnimateChangeMoney(button.GetComponent<UIWidget>(), walletManager.CurrencyCount(currencyType2) - item.getsDollars, walletManager.CurrencyCount(currencyType2), null);
			yield return new WaitForSeconds(0.75f);
		}
		if (item.getsTokens > 0)
		{
			mainNavigation.SetHeader(TableTennisMainNavigation.HeaderType.Diamonds);
			CurrencyType currencyType = CurrencyType.diamonds;
			TableTennisCoinController coinControl = mainNavigation.GetCoinControlForCurrency(currencyType);
			coinControl.AnimateChangeMoney(button.GetComponent<UIWidget>(), walletManager.CurrencyCount(currencyType) - item.getsTokens, walletManager.CurrencyCount(currencyType), null);
			yield return new WaitForSeconds(0.75f);
		}
		yield return new WaitForSeconds(1f);
		outAnimation.Play();
		while (outAnimation.isPlaying)
		{
			yield return null;
		}
		mainNavigation.SetHeader(prevHeaderType);
		CallOnComplete(response);
	}

	public void OnRetry()
	{
		ShowRestoringPurchases("Retrying Purchase. ");
	}

	public void OnContactUs()
	{
		CallOnComplete(response);
		GGSupportMenu.instance.SubmitBugReport(GGPlayerSettings.instance.Model.name, GGServerConstants.instance.appName, BehaviourSingletonInit<GGServerRequestsBackend>.instance.cachedPlayerId);
	}

	public void OnClickSuccess()
	{
		GGInAppPurchase.instance.purchaseComplete(item.productId);
	}

	public void OnClickFail()
	{
		GGInAppPurchase.instance.purchaseFailed(item.productId);
	}
}
